<?php

class ShowNewsController extends Controller {

    public function actions() {
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xFFFFFF,
            ),
                // page action renders "static" pages stored under 'protected/views/site/pages'
                // They can be accessed via: index.php?r=site/page&view=FileName
        );
    }

    public function actionIndex($title = '') {
        if ($title == '')
            $this->redirect(array('/news'));

        if (isset($_POST['CommentForm'])) {

            $mod = new Comments;
            $mod->attributes = $_POST['CommentForm'];
            $mod->save();

            Yii::app()->user->setFlash('comment', 'Ваш комментарий добавлен и находится на модерации.');
        }

        $criteria = new CDbCriteria;
        $criteria->select = 'id, title, image, full_story, date';  // выбираем только поле 'id,title'
        $criteria->condition = 'alt_title=:title';
        $criteria->params = array(':title' => $title);

        $model = News::model()->find($criteria);
        $this->render('index', array('model' => $model));
    }

    public function actionCategory($title = '') {
        if ($title == '')
            $this->redirect(array('/news'));

        $model = CategoryNews::getId($title);
        $category_id = $model->id;

        $criteria = new CDbCriteria;
        $criteria->select = 'title, alt_title, image, short_story, date';  // выбираем только поле 'id,title'
        $criteria->condition = 'category_id=:category_id';
        $criteria->params = array(':category_id' => $category_id);
        $criteria->order = 'id DESC';

        $count = News::model()->count($criteria);
        $pages = new CPagination($count);
        $pages->pageSize = 5;
        $pages->applyLimit($criteria);

        $model = News::model()->findAll($criteria);
        $this->render('show_category', array('model' => $model, 'pages' => $pages));
    }

    // Uncomment the following methods and override them if needed
    /*
      public function filters()
      {
      // return the filter configuration for this controller, e.g.:
      return array(
      'inlineFilterName',
      array(
      'class'=>'path.to.FilterClass',
      'propertyName'=>'propertyValue',
      ),
      );
      }

      public function actions()
      {
      // return external action classes, e.g.:
      return array(
      'action1'=>'path.to.ActionClass',
      'action2'=>array(
      'class'=>'path.to.AnotherActionClass',
      'propertyName'=>'propertyValue',
      ),
      );
      }
     */
}
